#include "Console.h"

COORD Console::Getxy(void) const{
	CONSOLE_SCREEN_BUFFER_INFO bInfo;
	GetConsoleScreenBufferInfo(hSTDHANDLE,&bInfo);
	return bInfo.dwCursorPosition;
}

bool Console::Gotoxy(short x,short y) const{
	COORD crdXY = {x,y};
	return SetConsoleCursorPosition(hSTDHANDLE,crdXY);
}

void Console::SetProgress(int nPos,int nSplit){
	std::ostringstream ostr;
	ostr<<"[";
	for(int nCount = 0;nCount < nPos/(nSplit/10);++nCount){
		ostr<<cProgressFlag;
	}
	for(int nCount = 0;nCount < 10 - nPos/(nSplit/10);++nCount){
		ostr<<cProgressFlagLess;
	}
	ostr<<"]("<<nPos<<"/"<<nSplit<<")";
	strProgress = ostr.str();
	nProgressLength = strProgress.size();
}

long Console::SetGlobalColor(short sForeg,short sBackg) const{
	std::ostringstream strCmd;
	strCmd<<"color "<<std::hex<<sBackg<<std::hex<<sForeg;
	return system(strCmd.str().c_str());
}

bool Console::SetColor(WORD wForegColor,WORD wBackgColor){
	return SetConsoleTextAttribute(hSTDHANDLE,wForegColor|wBackgColor);
}

bool Console::SetTitle(std::string& strTitle){
	return SetConsoleTitle(strTitle.c_str());
}

std::string Console::GetTitle(void) const{
	TCHAR lpszTitle[100] = {0};
	GetConsoleTitle(lpszTitle,sizeof(lpszTitle));
	return std::string(lpszTitle);
}

void Console::ClearScr(void) const{
	system("cls");
}

void Console::InitProgress(void){
	SetProgress(0,10);
	std::cout<<strProgress;
	bProgressInit = true;
}

void Console::SetProgressStyle(char cFlag,char cFlagLess){
	cProgressFlag = cFlag;
	cProgressFlagLess = cFlagLess;
}

void Console::ShowProgress(void) const{
	if(!bProgressInit)
		return;
	DeleteProgress();
	std::cout<<strProgress;
}

void Console::ShowProgress(int nPos,int nSplit){
	if(!bProgressInit)
		return;
	DeleteProgress();
	SetProgress(nPos,nSplit);
	std::cout<<strProgress;
}

void Console::DeleteProgress(void) const{
	COORD crdXY = Getxy();
	Gotoxy(crdXY.X - nProgressLength,crdXY.Y);
	for(int nCount = 0;nCount < nProgressLength;++nCount)
		std::cout<<" ";
	Gotoxy(crdXY.X - nProgressLength,crdXY.Y);
}
